# Spells interface for Warhammer Mod
# Made by Lord Olleus
#Last edited 30th June 06

from CvPythonExtensions import *
import CvUtil
import sdToolKit
import CvMagicSpells
import popup as PyPopup
import CvMagicUtils

# globals
gc = CyGlobalContext()
sd = sdToolKit
Range = CvMagicSpells.RangeSpells()
Spell = CvMagicSpells.Spells()
Select = CvMagicSpells.UnitSelectSpells()

#Called everytime a spell has been cast
#Return 0 to cancel
#Return 1 to show animation

def onCastSpell (argsList):
	pSpellUnit = argsList[0] # The book used to cast the spell
	szSpellName = pSpellUnit.getNameKey() # Might not work if spellbook has a custom name. Will change this later.
	
	#All 'remain in play' spells removed
	pCaster = pSpellUnit.getTransportUnit()
	pCaster.changeMoves(60)
	pCaster.setHasPromotion(gc.getInfoTypeForString("PROMOTION_FLAMING_SWORD"), false)
	
	if szSpellName == "TXT_KEY_UNIT_FLAMING_SWORD":
		Spell.FlamingSword(pSpellUnit)
		return


#Works in the same way as above but is called by spells
# which require a square to be chosen by the player.
def onCastRangeSpell (argsList):
	iX = argsList[0] # The X and Y coordinates of the chosen square.
	iY = argsList[1]
	pSpellUnit = argsList[2]
	szSpellName = pSpellUnit.getNameKey()
	
	#All 'remain in play' spells removed
	pCaster = pSpellUnit.getTransportUnit()
	pCaster.setHasPromotion(gc.getInfoTypeForString("PROMOTION_FLAMING_SWORD"), false)
		
	if szSpellName == "TXT_KEY_UNIT_BURNING_HEAD":
		Range.BurningHead(iX, iY, pSpellUnit)
		return		
		
	elif szSpellName == "TXT_KEY_UNIT_CONFLAGRATION_OF_DOOM":
		Range.ConflagrationOfDoom(iX, iY, pSpellUnit)
		return

	elif szSpellName == "TXT_KEY_UNIT_WALL_OF_FIRE":
		Range.WallOfFire(iX, iY, pSpellUnit)
		return		
		
	elif szSpellName == "TXT_KEY_UNIT_TRANSMUTATION_OF_LEAD":
		Range.TransmutationOfLead(iX, iY, pSpellUnit)
		return
	
	#The following call a popup
	elif szSpellName == "TXT_KEY_UNIT_FIREBALL":
		CvMagicUtils.UnitSelectionPopup(pSpellUnit, iX, iY)
		return
		
	elif szSpellName == "TXT_KEY_UNIT_FIERY_BLAST":
		CvMagicUtils.UnitSelectionPopup(pSpellUnit, iX, iY)
		return

	elif szSpellName == "TXT_KEY_UNIT_BURNING_IRON":
		CvMagicUtils.UnitSelectionPopup(pSpellUnit, iX, iY)
		return

	elif szSpellName == "TXT_KEY_UNIT_COMMANDMENT_OF_BRASS":
		CvMagicUtils.UnitSelectionPopup(pSpellUnit, iX, iY)
		return


#This is for ranged spells that require a specific unit to be selected.
#For the human player, this is called through a poopup on CvScreensInterface,
# For the AI, it is called directly from the SDK
def onUnitSelectSpell (argsList):
	pSpellUnit = argsList[0]
	pTarget = argsList[1]
	szSpellName = pSpellUnit.getNameKey()

	#All 'remain in play' spells removed
	pCaster = pSpellUnit.getTransportUnit()
	pCaster.setHasPromotion(gc.getInfoTypeForString("PROMOTION_FLAMING_SWORD"), false)
	
	if szSpellName == "TXT_KEY_UNIT_FIREBALL":
		Select.FireBall(pSpellUnit, pTarget)
		return
		
	elif szSpellName == "TXT_KEY_UNIT_FIERY_BLAST":
		Select.FieryBlast(pSpellUnit, pTarget)
		return

	elif szSpellName == "TXT_KEY_UNIT_BURNING_IRON":
		Select.BurningIron(pSpellUnit, pTarget)
		return

	elif szSpellName == "TXT_KEY_UNIT_COMMANDMENT_OF_BRASS":
		Select.CommandmentOfBrass(pSpellUnit, pTarget)
		return
		